package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.exception.BaseException;
import com.sky.exception.InvalidFormatException;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
@RestController("adminShopController")
public class ShopController {

    public static final String KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("管理端获取营业状态")
    @GetMapping("/status")
    public Result<Integer> getStatus(){
        Integer shopStatus = (Integer) redisTemplate.opsForValue().get(KEY);
        if (shopStatus == null) {
            shopStatus = 0;
        }
        log.info("获取到店铺的营业状态为:{}",shopStatus == 0 ?"打样中":"营业中");
        return Result.success(shopStatus);
    }


    @ApiOperation("设置营业状态")
    @PutMapping("{status}")
    public Result setStatus(@PathVariable Integer status){
        if (status == null) {
            throw new BaseException(MessageConstant.EXIT_NULL_DATA);
        }
        if (status != 0 && status != 1) {
            throw new InvalidFormatException(MessageConstant.NOT_ALLOW_DATA);
        }
        log.info("设置店铺的营业状态为:{}",status == 1?"营业中":"打样中");
        redisTemplate.opsForValue().set(KEY,status);
        return Result.success();
    }

}
